<template>
  <div class="app-container">
    <el-radio-group v-model="tabPosition" @change="tabChange" style="margin-bottom: 30px;">
      <el-radio-button :label="1">已校验</el-radio-button>
      <el-radio-button :label="2">待审核</el-radio-button>
      <el-radio-button :label="3">审核失败</el-radio-button>
      <el-radio-button :label="4">普通车辆</el-radio-button>
      <el-radio-button :label="5">新能源车辆</el-radio-button>
      <el-radio-button :label="6">黄牌车辆</el-radio-button>
    </el-radio-group>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="车牌号码" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :key="tabPosition" :data="spparkplaceList" >
      <el-table-column
      v-for="(item,index) in tableList"
      :key="index"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'userLoginChannel'" :options="dict.type.ELoginChannel" :value="row[item.prop]"/>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="tabPosition != 1 && tabPosition != 2" align="center" label="审核人员" prop="verifyBy"/>
      <el-table-column
        align="center"
        width="360"
        label="操作">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-zoom-in"
            @click="handleExamine(row)"
            v-hasPermi="['admin:userCar:verify']"
            v-if="tabPosition==2"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-view"
            v-hasPermi="['admin:userCar:detail']"
            @click="handleCheck(row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            v-hasPermi="['admin:userCar:delete']"
            @click="handleDelete(row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情弹框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div class="diy-mx-8">
        <div>
          <el-row class="diy-text-sm diy-leading-10" :gutter="10">
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                电话号码：{{ detail.phoneNumber }}
              </div>
            </el-col>
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                车牌号码：{{ detail.plateNumber }}
                <span class="diy-text-danger diy-cursor-pointer diy-ml-2.5" 
                v-if="tabPosition > 3" @click="revisePlate(detail)">修改</span></div>
            </el-col>
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                录入时间：{{ detail.createTime }}
              </div>
            </el-col>
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                审核时间：{{ detail.verifyTime }}
              </div>
            </el-col>
          </el-row>
          <div v-if="imgList.length > 0">
            <el-carousel indicator-position="outside" height="450px">
              <el-carousel-item v-for="(item,index) in imgList" :key="index">
                <el-image :src="item" class="diy-h-full" :preview-src-list="imgList"></el-image>
              </el-carousel-item>
            </el-carousel>
          </div>
          <div v-else>
            <el-empty description="暂无图片"></el-empty>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核弹框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="openExamine" width="1000px" append-to-body>
      <div class="diy-mx-8">
        <div >
          <div class="flex_hend">
            <div @click="getPore" style="color:#7478E6;cursor: pointer;font-size:16px;margin-bottom:10px;">
            查看已认证图片
          </div>
          </div>
          <el-row class="diy-text-sm diy-leading-10" :gutter="10">
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                用户手机：{{ detail.phoneNumber }}
              </div>
            </el-col>
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <div>
                  <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                  车牌号码：
                </div>
                <div>
                  <el-input v-model="form.plateNumber"></el-input>
                </div>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="diy-flex flex_hcneter">
                <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1' style="font-size:20px"/>
                录入时间：{{ detail.createTime }}
              </div>
            </el-col>
          </el-row>
        </div>
        <div >
          <div v-if="imgList.length > 0">
            <el-carousel indicator-position="outside" height="450px">
              <el-carousel-item v-for="(item,index) in imgList" :key="index">
                <el-image :src="item" class="diy-h-full" :preview-src-list="imgList"></el-image>
              </el-carousel-item>
            </el-carousel>
          </div>
          <div v-else>
            <el-empty description="暂无图片"></el-empty>
          </div>
        </div>
        <!---审核表单-->
        <div class="diy-mt-4">
          <el-form ref="form" :model="form" :rules="rules" label-width="88px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="是否通过" prop="passFlag">
                  <el-radio-group v-model="form.passFlag">
                    <el-radio
                      v-for="option in dict.type.EYesOrNo"
                      :key="option.value"
                      :label="Number(option.value)"
                    >{{ option.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="车辆类型" prop="type">
                  <el-radio-group v-model="form.type">
                    <el-radio
                      v-for="option in dict.type.EUserCarType"
                      :key="option.value"
                      :label="Number(option.value)"
                    >{{ option.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="意见">
                  <el-input v-model="form.verifyRemark" type="textarea" placeholder="请输入"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 认</el-button>
        <el-button @click="openExamine = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 修改车牌号弹框 -->
    <el-dialog :close-on-click-modal="false" :title="titlePlate" :visible.sync="openPlate" width="600px" append-to-body>
      <el-form ref="form_plate" :model="form_plate" :rules="rules_plate" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="车牌号" prop="plateNumber">
              <el-input v-model="form_plate.plateNumber" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPlate">确 定</el-button>
        <el-button @click="openPlate = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-image-viewer 
    v-if="showViewer"
    :url-list="srcList"
    :initialIndex="initialIndex"
    :zIndex='9999'
    :on-close="closeViewer">
  </el-image-viewer>

  </div>
</template>

<script>
import {fetchList,userCarDetail,delUserCar,updatePlateNumber,verifyUserCar} from "@/api/carOwners/plateManage";
export default {
  name: "plateManage",
  dicts: ['EYesOrNo','EUserCarType','ELoginChannel'],
  components:{},
  data() {
    return {
      showViewer: false,//取证图片显示
      initialIndex:0,//取证图片下标
      srcList:[],//取证图片
      tabPosition:1,
      tableList:[
        {label:'车牌号码',prop:'plateNumber',},
        {label:'电话号码',prop:'phoneNumber',},
        {label:'验证状态',prop:'checkStatusStr',},
        {label:'渠道',prop:'userLoginChannel',},
      ],//表格字段显示
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位信息表格数据
      spparkplaceList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
      },
      // 弹出层标题
      title: "",
      titlePlate: "",
      // 是否显示弹出层
      open: false,
      //审核弹出层
      openExamine: false,
      openPlate: false,
      //详情数据
      detail: {},
      //认证图片
      imgList: [],
      // 表单参数
      form: {carType: 1, isPass: 1, tip: ''},
      form_plate: {},
      // 表单校验
      rules: {
        type: [
          {required: true, message: '请选择车辆类型', trigger: 'change'}
        ],
        passFlag: [
          {required: true, message: '请选择是否通过', trigger: 'change'}
        ]
      },
      rules_plate: {
        plateNumber: [
          {required: true, message: "车牌号不能为空", trigger: "blur"}
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeViewer() {
      this.showViewer = false;
    },
    getPore(){//查询已认证图片
      if(!this.srcList.length) return this.$modal.msgWarning("暂无已认证图片");
      this.showViewer = true
    },
    //修改车牌号按钮
    revisePlate(detail) {
      this.openPlate = true;
      this.titlePlate = '修改车牌号';
      this.form_plate = {
        plateNumber:detail.plateNumber,
        id:detail.id
      }
    },
    //删除按钮
    handleDelete(row) {
      const carId = row.id
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
        return delUserCar(carId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 查看详情
    handleCheck(row) {
      userCarDetail(row.id).then(response => {
        this.open = true;
        this.title = '详情信息';
        let data = response.data || {};
        this.detail = data
        this.imgList = data.verifyImgUrls || [];
      });
    },
    //审核弹窗
    handleExamine(row) {
      this.reset()
      userCarDetail(row.id).then(response => {
        this.openExamine = true;
        this.title = '信息审核';
        let data = response.data || {}
        this.detail = data;
        this.imgList = data.verifyImgUrls || [];
        this.srcList = data.approveImgUrls || [];
        this.form = {
          id:row.id,
          passFlag:1,
          type:1,
          plateNumber:data.plateNumber,
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    //审核确认
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          verifyUserCar(this.form).then((response) => {
            this.$modal.msgSuccess("保存成功");
            this.openExamine = false;
            this.getList();
          });
        }
      });
    },
    //保存修改车牌号
    submitPlate() {
      this.$refs["form_plate"].validate(valid => {
        if (valid) {
          updatePlateNumber(this.form_plate).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.handleCheck(this.detail);
            this.getList();
            this.openPlate = false;
          });
        }
      });
    },
    tabChange(){//tab改变
      this.queryParams.page = 1
      this.getList()
    },
    /** 查询车位信息列表 */
    async getList() {
      let queryParams = {
        ...this.queryParams
      }
      this.loading = true;
      if(this.tabPosition === 1) queryParams.checkoutFlag = 1
      if(this.tabPosition === 2) queryParams.approveStatus = 1
      if(this.tabPosition === 3) queryParams.approveStatus = 2
      if(this.tabPosition === 4) {
        queryParams.approveStatus = 3
        queryParams.type = 1
      }
      if(this.tabPosition === 5) {
        queryParams.approveStatus = 3
        queryParams.type = 2
      }
      if(this.tabPosition === 6) {
        queryParams.approveStatus = 3
        queryParams.type = 3
      }
      fetchList(queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        let obj = rows[0] || {}
        this.totalDebtAmount = obj.totalDebtAmount || 0
        this.spparkplaceList = rows
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
<style lang="css">
/deep/ .el-image-viewer__wrapper {
  z-index: 3000 !important; /* 设置较高的堆叠上下文（z-index） */
}
</style>
<style lang="scss">
#container {width:650px; height: 300px; }
</style>
